package com.cn.hotel.dao.Impl;

import com.cn.hotel.dao.IRoomTypeDao;
import com.cn.hotel.model.RoomTypeModel;
import com.cn.hotel.model.ServiceItemsModel;
import com.cn.hotel.util.JDBC;
import com.mysql.cj.protocol.ResultsetRow;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class RoomtypeImpl implements IRoomTypeDao {
    @Override
    public List<RoomTypeModel> findAll(RoomTypeModel roomTypeModel) {
        StringBuilder sql = new StringBuilder("select * from roomtype where 1 = 1");
        if (roomTypeModel.getName() != null && !"".equals(roomTypeModel.getName())) {
            sql.append(" and name like '%" + roomTypeModel.getName() + "%'");
        }
        sql.append(" order by id asc limit " + (roomTypeModel.getPageModel().getPageNum() - 1) * roomTypeModel.getPageModel().getPageSize() + "" +
                "," + roomTypeModel.getPageModel().getPageSize() + "");
        ResultSet resultSet = JDBC.getResultSet(sql.toString());
        List<RoomTypeModel> list = new ArrayList<>();
        try {
            while (resultSet.next()) {
                RoomTypeModel model = new RoomTypeModel();
                model.setId(resultSet.getInt("id"));
                model.setName(resultSet.getString("name"));
                model.setHref(resultSet.getString("href"));
                list.add(model);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public int getNumber(RoomTypeModel roomTypeModel) {
        StringBuilder sql = new StringBuilder("select count(*) count from roomtype where 1 = 1");
        if (roomTypeModel.getName() != null && !"".equals(roomTypeModel.getName())) {
            sql.append(" and name like '%" + roomTypeModel.getName() + "%'");
        }
        ResultSet resultSet = JDBC.getResultSet(sql.toString());
        int count = 0;
        try {
            resultSet.next();
            count = resultSet.getInt(1);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }

    /**
     * 增加
     *
     * @param roomTypeModel
     * @return
     */
    @Override
    public int add(RoomTypeModel roomTypeModel) {
        StringBuilder sql = new StringBuilder();
        if (!"".equals(roomTypeModel.getName()) && !"".equals(roomTypeModel.getHref())) {
            sql.append("insert into roomtype(name,href) values('" + roomTypeModel.getName() + "'," +
                    "'" + roomTypeModel.getHref() + "')");
        }
        return JDBC.executeUpdate(sql.toString());
    }

    /**
     * 通过id查询信息
     *
     * @param temp
     * @return
     */
    @Override
    public RoomTypeModel finById(Integer temp) {
        StringBuilder sql = new StringBuilder();
        if (temp != 0) {
            sql.append("select * from roomtype where id=" + temp);
        }
        ResultSet resultSet = JDBC.getResultSet(sql.toString());
        RoomTypeModel model = new RoomTypeModel();
        try {
            while (resultSet.next()) {
                model.setId(resultSet.getInt("id"));
                model.setName(resultSet.getString("name"));
                model.setHref(resultSet.getString("href"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return model;
    }

    /**
     * 保存编辑
     *
     * @param roomTypeModel
     * @return
     */
    @Override
    public int edit(RoomTypeModel roomTypeModel) {
        StringBuilder sql = new StringBuilder("update roomtype set ");
        if (roomTypeModel.getName() != null && !"".equals(roomTypeModel.getName())) {
            sql.append(" name='" + roomTypeModel.getName() + "',");
        }
        if (roomTypeModel.getHref() != null && !"".equals(roomTypeModel.getHref())) {
            sql.append(" href='" + roomTypeModel.getHref() + "',");
        }
        sql.append(" status = '' where id = "+roomTypeModel.getId()+"");
        return JDBC.executeUpdate(sql.toString());
    }

    @Override
    public int delete(Integer aa) {
        StringBuilder sql = new StringBuilder();
        if (aa!=0){
            sql.append("delete  from roomtype where id="+aa);
        }
        return JDBC.executeUpdate(sql.toString());
    }
//------------------下面是前端页面调取数据逻辑代码------------------------

    /**
     * 网站首页调取
     * @return
     */
    @Override
    public List<RoomTypeModel> getRoomTypeModelList() {
        StringBuilder sql = new StringBuilder("select * from roomtype");
        ResultSet resultSet = JDBC.getResultSet(sql.toString());
        List<RoomTypeModel> list = new ArrayList<>();
        try {
            while (resultSet.next()) {
                RoomTypeModel model = new RoomTypeModel();
                model.setName(resultSet.getString("name"));
                model.setHref(resultSet.getString("href"));
                list.add(model);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
}
